package com.kiln.juxingqs.fragment

import android.view.View
import client.global.commlib.base.BaseFragment
import client.global.commlib.dialog.CameraOrSelectDialog
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.FragmentSetxxtwoBinding
import com.kiln.juxingqs.ui.vm.SetskxxViewModel

class SetxxTwoFragment : BaseFragment<FragmentSetxxtwoBinding, SetskxxViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_setxxtwo
    }

    override fun initViewModel(): SetskxxViewModel = createViewModel()

    override fun initWidget() {
        binding.event = this
        binding.vm = viewModel
        viewModel.callTwo.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    requireActivity().finish()
                } else {
                    toast(it.message) //todo toast fail
                }
            }
        })

        binding.emptyLayout.setOnClickListener {
            viewModel.callTwo()
        }

        viewModel.dataTwo.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    it.data?.pay_list?.let {
                        if (it.size > 0) {
                            viewModel.payCardTwo.value = it.get(0).pay_card
                            mImgLoader.load(it.get(0).pay_img)
                                .into(binding.idFontImg)
                            viewModel.nameTwo.value = it.get(0).pay_name
                            viewModel.imgTwoOld.value = it.get(0).pay_img
                            viewModel.realNameTwo.value = it.get(0).real_name
                        }
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    toast(it.message) //todo toast fail
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        viewModel.callTwo()
    }

    fun imgSelect() {
//        SelectPhotoActivity.show(requireActivity(), SelectOptions().apply {
//            mSelectCount = 1
//            mCallback = object : SelectOptions.Callback {
//                override fun doSelected(images: ArrayList<Uri>?) {
//                    images?.let {
//                        viewModel.imgTwo.value = it[0]
//                        mImgLoader.load(it[0]).into(binding.idFontImg)
//                    }
//                }
//            }
//        })

        CameraOrSelectDialog(requireActivity()).apply {
            suc = {
                viewModel.imgTwo.value = it
                mImgLoader.load(it).into(this@SetxxTwoFragment.binding.idFontImg)
            }
        }.show()
    }

}